import { Injectable , Inject } from '@angular/core';
import { Component, ViewEncapsulation } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { HttpClientHelper } from './app.httpclient';

import 'rxjs/Rx';
// import localStorage from 'localStorage';

@Injectable()
export class UserService {
  private loginUrl: string = 'users/login';
  private registerUrl: string = 'users/register';
  private logoutUrl: string = 'users/logout';
  private userForgotUrl: string = 'users/forgotpassword';
  private userResetPassUrl: string = 'users/passwordreset?user_id=';
  private facebookLoginUrl: string = 'auth/facebook/token?access_token=';
  private userIdeaurl = 'ideas/createIdea';
  private getIdeaByTitle = 'ideas/getTitle';
  private listIdeaurl = 'ideas/getIdea';
  private ideaLikesurl = 'votes/addVote';
  private favouriteIdea = 'ideas/favouriteIdea';
  private getCommentIdea = 'comments/getCommentsByIdea';
  private addCommentIdea = 'comments/addComment';
  private httpClient: any;

  constructor (httpClient: HttpClientHelper) {
        this.httpClient = httpClient;
  }

  private extractResponse(res: JSON) {
    return res;
  }

  private handleError (error: any) {
    return Observable.throw(error);
  }

  //For signup service
  userSignup (data): Observable<any> {
    return this.httpClient.postPreLogin(this.registerUrl, data)
                    .map(this.extractResponse)
                    .catch(this.handleError);
  }

  userLogin (data): Observable<any> {
    return this.httpClient.postPreLogin(this.loginUrl, data)
                    .map(this.extractResponse)
                    .catch(this.handleError);
  }

  //For signup service
  userLogout (data): Observable<any> {
    return this.httpClient.post(this.logoutUrl, data)
                    .map(this.extractResponse)
                    .catch(this.handleError);
  }

   //Send Forgot Password Link
   userForgot (data): Observable<any> {
     return this.httpClient.postPreLogin(this.userForgotUrl, data)
                    .map(this.extractResponse)
                    .catch(this.handleError);
  }

 //Send Reset password with new password
  userReset (data): Observable<any> {
    let headers = new Headers();
    this.userResetPassUrl = this.userResetPassUrl+ data.user_id;
    headers.append('Content-Type', 'application/json');
    headers.append('token',data.token);
    delete data.token;
    delete data.user_id;
    return this.httpClient.postByHeaderPreLogin(this.userResetPassUrl, data, headers)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  facebookLogin (data): Observable<any> {
     this.facebookLoginUrl = this.facebookLoginUrl + data.token;
    return this.httpClient.get(this.facebookLoginUrl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  //Post Idea
  userIdea (data): Observable<any>{
    return this.httpClient.post(this.userIdeaurl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  ideaList (data): Observable<any>{
    return this.httpClient.post(this.listIdeaurl, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getIdea (data): Observable<any> {
   return this.httpClient.post(this.getIdeaByTitle, data)
   .map(this.extractResponse)
   .catch(this.handleError);
   }

  ideaLikes (data): Observable<any> {
    return this.httpClient.post(this.ideaLikesurl, data)
    .map(this.extractResponse)
    .catch(this.handleError);
  }

  faouriteLikes (data): Observable<any> {
    return this.httpClient.post(this.favouriteIdea, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  getcardComment (data): Observable<any> {
    return this.httpClient.post(this.getCommentIdea, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

  addcardComment (data): Observable<any> {
    return this.httpClient.post(this.addCommentIdea, data)
      .map(this.extractResponse)
      .catch(this.handleError);
  }

}
